package com.bwie.goods.service.impl;

import com.bwie.common.domain.Shop;
import com.bwie.common.domain.Shopping;
import com.bwie.common.result.Result;
import com.bwie.goods.mapper.ShopMapper;
import com.bwie.goods.service.ShopService;
import com.bwie.goods.util.FastUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class ShopServiceimpl implements ShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private FastUtil fastUtil;

    @Override
    public Result<List<Shop>> list() {
        List<Shop> list = shopMapper.list();
        return Result.success(list);
    }

    @Override
    public Result upload(MultipartFile file) {
        String upload="";

        try {
            upload = fastUtil.upload(file);
            if(upload==null){
                return Result.error("图片上传失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.success("http://1.95.38.198:8888/"+upload,"上传成功");
    }

    @Override
    public Result ShoppingAdd(Shop shop) {
        //查询购物车信息
        Shopping shoppings=shopMapper.shoppingList();
        //判断商品中数量不能小于等于0
        if(shop.getNumber()<=0){
            return Result.error("该商品已经卖完");
        }
        //查询购物车中是否有该商品 有则添加数量 没有则添加
        if(shoppings!=null){
            if(shoppings.getName().equals(shop.getName())){
                Integer number = shoppings.getNumber();
                number++;
                shoppings.setNumber(number);
                Integer i = shopMapper.ShoppingUpdate(shoppings);

                //查询商品信息
                Shop shops = shopMapper.ShoppingList(shop.getId());

                //商品减存库
                shopMapper.updateShopNumber(shops);

                return Result.success(i>0?200:500,i>0?"已存在该商品,数量以加一":"添加失败");

            }

        }


        //购物车添加
        Shopping shopping = new Shopping();
        shopping.setId(shop.getId());
        shopping.setName(shop.getName());
        shopping.setMoney(shop.getMoney());
        shopping.setType(shop.getType());
        shopping.setStats(shop.getStats());
        shopping.setShopBreak(shop.getShopBreak());
        shopping.setImg(shop.getImg());
        shopping.setNumber(1);
        shopping.setDiscountsMoney(shop.getDiscountsMoney());
        Integer i = shopMapper.ShoppingAdd(shopping);

        //查询商品信息
        Shop shops = shopMapper.ShoppingList(shop.getId());

        //商品减存库
        shopMapper.updateShopNumber(shops);

        return Result.success(i>0?200:500,i>0?"添加成功":"添加失败");
    }

    @Override
    public Result ShopUpFlag(Integer id) {
        //查询商品信息
        Shop shop=shopMapper.findByFlagId(id);
        if(shop.getFlag()==1){
            return Result.error("该商品已上架");
        }
        Integer i=shopMapper.ShopUpFlag(id);
        return Result.success(i>0?200:500,i>0?"上架成功":"上架失败");
    }

    @Override
    public Result ShopUpdateFlag(Integer id) {
        //查询商品信息
        Shop shop=shopMapper.findByFlagId(id);
        if(shop.getFlag()==2){
            return Result.error("该商品已下架");
        }
        Integer i=shopMapper.ShopUpdateFlag(id);
        return Result.success(i>0?200:500,i>0?"下架成功":"下架失败");
    }
}
